var live = '泊头';

function loadXMLDoc() {
	var xmlhttp;
	if(window.XMLHttpRequest) {
		//  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
		xmlhttp = new XMLHttpRequest();
	} else {
		// IE6, IE5 浏览器执行代码
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var json = JSON.parse(xmlhttp.responseText);
			console.log(json);
			var wli = document.querySelectorAll('#weather li');
			//时间
			for(var k = 0; k < wli.length; k++){
				wli[k].children[0].innerHTML = json.daily_forecast[k].date.slice(5,10);
			}
			
			//天气
			for(var i = 0; i < wli.length; i++) {
				//天气
				wli[i].children[1].innerHTML = json.daily_forecast[i].cond.txt_n;
				//摄氏度
				wli[i].children[2].children[0].innerHTML = json.daily_forecast[i].tmp.max;
				wli[i].children[2].children[1].innerHTML = json.daily_forecast[i].tmp.min;
				//风
				wli[i].children[3].innerHTML = json.daily_forecast[i].wind.sc + '级';
			}
			//分时段报告
			var rli = document.querySelectorAll('#report li');
			for(var j = 0; j < rli.length; j++) {
				//时间
				rli[j].children[0].innerHTML = json.hourly_forecast[j].date.slice(11, 13) + "时";
				//摄氏度
				rli[j].children[1].children[0].innerHTML = json.hourly_forecast[j].tmp;
				//天气
				rli[j].children[2].innerHTML = json.hourly_forecast[j].wind.dir;
				//风速
				rli[j].children[3].innerHTML = json.hourly_forecast[j].wind.sc + '级';

			}

			//生活指数
			var lli = document.querySelector('#live');
			//紫外线指数
			lli.children[0].children[1].innerHTML = json.suggestion.uv.brf;
			lli.children[0].children[2].innerHTML = json.suggestion.uv.txt;
			//减肥指数
			lli.children[1].children[1].innerHTML = json.suggestion.sport.brf;
			lli.children[1].children[2].innerHTML = json.suggestion.sport.txt;
			//血糖指数
			lli.children[2].children[1].innerHTML = json.suggestion.flu.brf;
			lli.children[2].children[2].innerHTML = json.suggestion.flu.txt;
			//穿衣指数
			lli.children[3].children[1].innerHTML = json.suggestion.drsg.brf;
			lli.children[3].children[2].innerHTML = json.suggestion.drsg.txt;
			//洗车指数
			lli.children[4].children[1].innerHTML = json.suggestion.cw.brf;
			lli.children[4].children[2].innerHTML = json.suggestion.cw.txt;
			//空气污染扩散指数
			lli.children[5].children[1].innerHTML = json.suggestion.air.brf;
			lli.children[5].children[2].innerHTML = json.suggestion.air.txt;
		}
	}
	xmlhttp.open("GET", "https://bird.ioliu.cn/weather?city=" + live, true);
	xmlhttp.send();
}

loadXMLDoc();

document.querySelector('#inp').addEventListener('change', function() {
	console.log(this.value);
	live = this.value;
	loadXMLDoc();
})